
package com.epam.androids.training.day10;

import android.app.Activity;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

/**
 * Demonstrate shared preferences work with listener and some work with sqllite
 * db
 * 
 * @author Natalie
 */
public class StorageDemoActivity extends Activity {

  public static final String PREFS_NAME = "PrefDemoFile";

  /** some fake data for filling db */
  public static final String SENCHA_TEA = "sencha";

  public static final String BANCHA_TEA = "bancha";

  public static final String ASSAM_TEA = "assam";

  public static final String MILK_OOLONG_TEA = "milk oolong";

  public static final String GREEN = "green";

  public static final String BLACK = "black";

  public static final String OOLONG = "oolong";

  private static final String ENABLED_BTN = "enabledBtn";

  private static final String TITLE = "Teas:\n\n";

  private boolean enableButton;

  private CheckBox enableBtn;

  private Button btn;

  private Button deleteBtn;

  private TeaOpenHelper teaData;

  private TextView teaView;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    teaView = (TextView) findViewById(R.id.tea_view);
    // Restore preferences
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    // Use instance field for listener
    // It will not be gc'd as long as this instance is kept referenced
    OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
      @Override
      public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (key.equals(ENABLED_BTN)) {
          btn.setEnabled(prefs.getBoolean(key, false));
        }
      }
    };

    settings.registerOnSharedPreferenceChangeListener(listener);

    enableButton = settings.getBoolean(ENABLED_BTN, false);
    btn = (Button) findViewById(R.id.button1);
    btn.setEnabled(enableButton);
    btn.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View arg0) {
        addTea(SENCHA_TEA, GREEN);
        addTea(ASSAM_TEA, BLACK);
        addTea(MILK_OOLONG_TEA, OOLONG);
        addTea(BANCHA_TEA, GREEN);
        Cursor cursor = getTeas();
        showTeas(cursor);
      }
    });

    deleteBtn = (Button) findViewById(R.id.delete_btn);

    deleteBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {
        Cursor cursor = getTeas();
        removeTea(SENCHA_TEA);
        removeTea(ASSAM_TEA);
        removeTea(BANCHA_TEA);
        showTeas(cursor);
      }
    });

    enableBtn = (CheckBox) findViewById(R.id.enable_btn_chck);
    enableBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(ENABLED_BTN, enableBtn.isChecked());
        editor.commit();

      }
    });

    teaData = new TeaOpenHelper(getApplicationContext());

  }

  /**
   * Add item to db
   * 
   * @param teaName
   * @param teaSort
   */
  private void addTea(String teaName, String teaSort) {
    SQLiteDatabase db = teaData.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(TeaOpenHelper.TEA_NAME, teaName);
    values.put(TeaOpenHelper.TEA_SORT, teaSort);
    db.insert(TeaOpenHelper.TEA_TABLE_NAME, null, values);
  }

  /**
   * remove item from db
   * 
   * @param teaName
   */
  private void removeTea(String teaName) {
    SQLiteDatabase db = teaData.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(TeaOpenHelper.TEA_NAME, teaName);
    db.delete(TeaOpenHelper.TEA_TABLE_NAME, TeaOpenHelper.TEA_NAME + "=?", new String[] {
      teaName
    });

  }

  /**
   * return cursor with queried data
   * 
   * @return Cursor
   */
  private Cursor getTeas() {
    SQLiteDatabase db = teaData.getReadableDatabase();
    Cursor cursor = db.query(TeaOpenHelper.TEA_TABLE_NAME, null, null, null, null, null, TeaOpenHelper.TEA_NAME);

    startManagingCursor(cursor);
    return cursor;
  }

  private void showTeas(Cursor cursor) {
    StringBuilder ret = new StringBuilder(TITLE);
    while (cursor.moveToNext()) {
      String name = cursor.getString(0);
      String sort = cursor.getString(1);
      ret.append(name + ": " + sort + "\n");
    }
    teaView.setText(ret);
  }

  @Override
  protected void onDestroy() {
    teaData.close();
  }
}
